//
//  ConsultingCell.m
//  HuiKaoClient
//
//  Created by 龚 杰 on 13-3-11.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "ConsultingCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation ConsultingCell
@synthesize height;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        m_headImageView = [[AsyncImageView alloc] init];
        [self.contentView addSubview:m_headImageView];
        m_MessagerView = [[UIView alloc] init];
        [self.contentView addSubview:m_MessagerView];
    }
    return self;
}
- (void)initDirection:(NSDictionary*)Data
{
    NSString* headurl = [Data objectForKey:@"avatar"];
    NSString* text = [Data objectForKey:@"message"];
    isDirection =[Data objectForKey:@"type"];
//    text =@"发撒旦法撒旦";
	CGSize textSize = {220.0, 20000.0};
	CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:textSize];
    NSInteger num = size.height/15.0f;
    NSLog(@"%d",num);
    
    CGRect iRect1;
    CGRect iRect;
    UIImageView* ibg;
    if ([isDirection isEqual:@"a"]) {
        m_headImageView.frame = CGRectMake(10, 10, 38, 38);
        if (num == 1) {
            if (text.length == 1) {
                iRect = CGRectMake(57, 10, 40, 40);
            }else{
                iRect = CGRectMake(57, 10, size.width+20, 40);
            }
        }else{
            iRect = CGRectMake(57, 10, size.width+20, size.height+10);
        }
        if (num == 1) {
 
           iRect1 = CGRectMake(15, 10, size.width,size.height);
        }else{
           iRect1 = CGRectMake(15, 5, size.width, size.height);
        }
       ibg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, iRect.size.width, iRect.size.height)];
       [ibg setImage:[[UIImage imageNamed:@"bg_chat_u.png"] stretchableImageWithLeftCapWidth:26 topCapHeight:24]];
    }else{
        m_headImageView.frame = CGRectMake(320-50, 10, 38, 38);
        if (num == 1) {
            if (text.length == 1) {
                iRect = CGRectMake(320-57 - (size.width+20)-10, 10, 40, 40);
            }else{
                iRect = CGRectMake(320-57 - (size.width+20), 10, size.width+20, 40);
            }
        }else{
            iRect = CGRectMake(320-57 - (size.width+20), 10, size.width+20, size.height+10);
        }
        if (num == 1) {
            iRect1 = CGRectMake(8, 10, size.width, size.height);
        }else{
            iRect1 = CGRectMake(8, 5, size.width, size.height);
        }
       ibg =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, iRect.size.width, iRect.size.height)];
       [ibg setImage:[[UIImage imageNamed:@"bg_chat_me.png"] stretchableImageWithLeftCapWidth:26 topCapHeight:24]];
    }
    
    //头像
    [m_headImageView setBackgroundColor:[UIColor blackColor]];
    m_headImageView.layer.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.45].CGColor;
    m_headImageView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    m_headImageView.layer.shadowOpacity = 1.0f;
    m_headImageView.layer.shouldRasterize = NO;
    m_headImageView.layer.shadowRadius = 2;
    
    if (headurl.length > 0) {
        m_headImageView._imageRequestURL = headurl;
        m_headImageView.m_BoolSetImage = YES;
        [m_headImageView customLoadImageFromURL];
    }else{
        [m_headImageView SetUIImage:[UIImage imageNamed:@"avatarsms.png"]];
    }
    
    //消息面板
    m_MessagerView.frame = iRect;
    m_MessagerView.layer.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.45].CGColor;
    m_MessagerView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    m_MessagerView.layer.shadowOpacity = 1.0f;
    m_MessagerView.layer.shouldRasterize = NO;
    m_MessagerView.layer.shadowRadius = 2;
    
    //文字
    UILabel* showText = [[UILabel alloc] initWithFrame:iRect1];
    showText.lineBreakMode   = UILineBreakModeWordWrap;
    showText.numberOfLines   = 0;
    showText.backgroundColor = [UIColor clearColor];
    showText.font            = [UIFont systemFontOfSize:15.0f];
    showText.textColor       = [UIColor blackColor];
    showText.text            = text;
    
    [ibg addSubview:showText];
    [showText release];
    [m_MessagerView addSubview:ibg];
    [ibg release];

    if (num == 1) {
        self.height = 60;
    }else{
     
        self.height =m_MessagerView.frame.size.height+20;
    }

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc{
    [super dealloc];
}
@end
